package com.hymjweb.demo.hymj.main_manage.service;

import com.hymjweb.demo.framework.cache.service.GlobalCacheService;
import com.hymjweb.demo.framework.menu.service.MenuService;
import com.hymjweb.demo.framework.sys.BaseService;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.master_bill_manage.flight.service.BMasterBillFlightService;
import com.hymjweb.demo.hymj.native_bill_manage.service.BNativeBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MainService extends BaseService {

    @Autowired
    private GlobalCacheService globalCacheService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private BNativeBillService bNativeBillService;

    @Autowired
    private BMasterBillFlightService bMasterBillFlightService;


    public Object getCache() {

        return globalCacheService.getCache();

    }

    public Object getMenuTree() {
        return menuService.getMenuAndChildrenList();
    }


    /**
     * 部门开单信息汇总
     * @param startDate
     * @param endDate
     * @return
     */
    public Object getDepartmentOpenBillEchartOption(String startDate, String endDate) {

        List<Map<String,Object>> mapList = bNativeBillService.queryDepartmentOpenBillCollectList(startDate,endDate);

        Map<String,Object> optionMap = new HashMap<>();

        Map<String,Object> titleMap = new HashMap<>();
        titleMap.put("text","部门开分单信息汇总");
        titleMap.put("subtext",startDate + " 至 " + endDate);

        optionMap.put("title",titleMap);

        Map<String,Object> tooltipMap = new HashMap<>();
        titleMap.put("trigger","axis");
        Map<String,Object> axisPointerMap = new HashMap<>();
        axisPointerMap.put("type","shadow");
        tooltipMap.put("axisPointer",axisPointerMap);

        optionMap.put("tooltip",tooltipMap);

        Map<String,Object> gridMap = new HashMap<>();
        gridMap.put("left","3%");
        gridMap.put("right","4%");
        gridMap.put("bottom","3%");
        gridMap.put("containLabel",true);

        optionMap.put("grid",gridMap);

        Map<String,Object> toolBoxMap = new HashMap<>();
        Map<String,Object> featureMap = new HashMap<>();
        featureMap.put("saveAsImage",new HashMap<String,Object>());
        toolBoxMap.put("feature",featureMap);
        optionMap.put("toolbox",toolBoxMap);


        Map<String,Object> yAxisMap = new HashMap<>();
        yAxisMap.put("type","value");
        yAxisMap.put("boundaryGap",new Double[]{0D,0.01});
        optionMap.put("yAxis",yAxisMap);


        Map<String,Object> legendMap = new HashMap<>();
        legendMap.put("data",new String[]{"件数","实重","计重","结算金额"});

        optionMap.put("legend",legendMap);

        List<String> departmentNameList = new ArrayList<>();// 部门名称
        List<Integer> totalNumberList = new ArrayList<>(); //  件数
        List<Double> totalActualWeightList = new ArrayList<>(); // 实重
        List<Double> totalChargeableWeightList = new ArrayList<>(); // 计重
        List<Double> totalSettlementAmountList = new ArrayList<>(); // 结算金额

        for(Map<String,Object> mapObj : mapList){
            String departmentName = DataTypeUtil.toStringObject(mapObj.get("openDepartmentName"));
            Integer totalNumber = DataTypeUtil.toInt(mapObj.get("totalNumber"));
            Double totalActualWeight = DataTypeUtil.toDouble(mapObj.get("totalActualWeight"));
            Double totalChargeableWeight = DataTypeUtil.toDouble(mapObj.get("totalChargeableWeight"));
            Double totalSettlementAmount = DataTypeUtil.toDouble(mapObj.get("totalSettlementAmount"));

            departmentNameList.add(departmentName);
            totalNumberList.add(totalNumber);
            totalActualWeightList.add(totalActualWeight);
            totalChargeableWeightList.add(totalChargeableWeight);
            totalSettlementAmountList.add(totalSettlementAmount);
        }

        Map<String,Object> xAxisMap = new HashMap<>();
        xAxisMap.put("type","category");
        xAxisMap.put("data",departmentNameList);
        optionMap.put("xAxis",xAxisMap);


        List<Object> objList = new ArrayList<>();

        Map<String,Object> totalNumberListMap = new HashMap<>();
        totalNumberListMap.put("name","件数");
        totalNumberListMap.put("type","bar");
        totalNumberListMap.put("data",totalNumberList);
        objList.add(totalNumberListMap);


        Map<String,Object> totalActualWeightListMap = new HashMap<>();
        totalActualWeightListMap.put("name","实重");
        totalActualWeightListMap.put("type","bar");
        totalActualWeightListMap.put("data",totalActualWeightList);
        objList.add(totalActualWeightListMap);


        Map<String,Object> totalChargeableWeightListMap = new HashMap<>();
        totalChargeableWeightListMap.put("name","计重");
        totalChargeableWeightListMap.put("type","bar");
        totalChargeableWeightListMap.put("data",totalChargeableWeightList);
        objList.add(totalChargeableWeightListMap);


        Map<String,Object> totalSettlementAmountListMap = new HashMap<>();
        totalSettlementAmountListMap.put("name","结算金额");
        totalSettlementAmountListMap.put("type","bar");
        totalSettlementAmountListMap.put("data",totalSettlementAmountList);
        objList.add(totalSettlementAmountListMap);


        optionMap.put("series",objList);

        return optionMap;
    }


    /**
     * 部门日开单量
     * @param startDate
     * @param endDate
     * @return
     */
    public Object getDepartmentDailyOpenBillEchartOption(String startDate, String endDate) {

        List<Map<String,Object>> mapList = bNativeBillService.queryDepartmentDailyOpenBillCollectList(startDate,endDate);

        Map<String,Object> optionMap = new HashMap<>();

        Map<String,Object> titleMap = new HashMap<>();
        titleMap.put("text","部门日开分单量");
        titleMap.put("subtext",startDate + " 至 " + endDate);
        optionMap.put("title",titleMap);

        Map<String,Object> tooltipMap = new HashMap<>();
        titleMap.put("trigger","axis");
        optionMap.put("tooltip",tooltipMap);

        Map<String,Object> gridMap = new HashMap<>();
        gridMap.put("left","3%");
        gridMap.put("right","4%");
        gridMap.put("bottom","3%");
        gridMap.put("containLabel",true);
        optionMap.put("grid",gridMap);

        Map<String,Object> toolBoxMap = new HashMap<>();
        Map<String,Object> featureMap = new HashMap<>();
        featureMap.put("saveAsImage",new HashMap<String,Object>());
        toolBoxMap.put("feature",featureMap);
        optionMap.put("toolbox",toolBoxMap);

        Map<String,Object> yAxisMap = new HashMap<>();
        yAxisMap.put("type","value");
        optionMap.put("yAxis",yAxisMap);

        Set<String> departmentNameSet = new HashSet<>();
        Set<String> openBillDateSet = new HashSet<>();

        // List转 map   后面备用
        Map<String,Map<String,Object>> departmentDailyOpenBillMap = new HashMap<>();

        for(Map<String,Object> mapObj : mapList){
            String departmentName = DataTypeUtil.toStringObject(mapObj.get("openDepartmentName"));
            String openBillDate = DataTypeUtil.toStringObject(mapObj.get("openBillDate"));
            openBillDateSet.add(openBillDate);
            departmentNameSet.add(departmentName);

            String key = departmentName + "," + openBillDate;
            departmentDailyOpenBillMap.put(key,mapObj);
        }

        Map<String,Object> legendMap = new HashMap<>();
        legendMap.put("data",departmentNameSet.toArray());
        optionMap.put("legend",legendMap);

        Map<String,Object> xAxisMap = new HashMap<>();
        xAxisMap.put("type","category");
        xAxisMap.put("boundaryGap",false);
        List<String> list1= new ArrayList<>(openBillDateSet);
        Collections.sort(list1);
        xAxisMap.put("data",list1);
        optionMap.put("xAxis",xAxisMap);

        List<Object> seriesArray = new ArrayList<>();
         for(String departmentName : departmentNameSet){
             List<Integer> departmentOpenNumberList = new ArrayList<>();
             for(String openBillDate : list1){
                 String key = departmentName + "," + openBillDate;
                 if(departmentDailyOpenBillMap.containsKey(key)){
                     Map<String,Object> mapObj = departmentDailyOpenBillMap.get(key);
                     departmentOpenNumberList.add(DataTypeUtil.toInt(mapObj.get("billNumber")));
                 }
                 else{
                     departmentOpenNumberList.add(0);
                 }
             }
             Map<String,Object> seriesArrayObjMap = new HashMap<>();
             seriesArrayObjMap.put("name",departmentName);
             seriesArrayObjMap.put("type","line");
             seriesArrayObjMap.put("stack","总量");
             seriesArrayObjMap.put("data",departmentOpenNumberList);
             seriesArray.add(seriesArrayObjMap);
         }
        optionMap.put("series",seriesArray);


        return optionMap;
    }

    /**
     * 主单航空公司走货主单数
     * @param startDate
     * @param endDate
     * @return
     */
    public Object showFlightAirlinesOpenBill(String startDate, String endDate) {
        List<Map<String,Object>> mapList = bMasterBillFlightService.queryFlightAirlinesOpenBillCollectList(startDate,endDate);


        Map<String,Object> optionMap = new HashMap<>();

        Map<String,Object> titleMap = new HashMap<>();
        titleMap.put("text","航空公司走货主单量统计");
        titleMap.put("subtext",startDate + " 至 " + endDate);
        titleMap.put("left","center");
        optionMap.put("title",titleMap);

        Map<String,Object> tooltipMap = new HashMap<>();
        titleMap.put("trigger","item");
        optionMap.put("tooltip",tooltipMap);

        Map<String,Object> toolBoxMap = new HashMap<>();
        Map<String,Object> featureMap = new HashMap<>();
        featureMap.put("saveAsImage",new HashMap<String,Object>());
        toolBoxMap.put("feature",featureMap);
        optionMap.put("toolbox",toolBoxMap);

        Map<String,Object> legendMap = new HashMap<>();
        legendMap.put("orient","vertical");
        legendMap.put("left","left");
        optionMap.put("legend",legendMap);

        List<Map<String,Object>> dataList = new ArrayList<>();
        for(Map<String,Object> mapObj : mapList){
            Map<String,Object> dataMapObj = new HashMap<>();
            String airlinesName = DataTypeUtil.toStringObject(mapObj.get("name"));
            Integer totalNumber = DataTypeUtil.toInt(mapObj.get("totalNumber"));
            dataMapObj.put("name",airlinesName);
            dataMapObj.put("value",totalNumber);
            dataList.add(dataMapObj);
        }

        Map<String,Object> seriesListMapObj = new HashMap<>();
        seriesListMapObj.put("name","航空公司");
        seriesListMapObj.put("type","pie");
        seriesListMapObj.put("radius","50%");
        seriesListMapObj.put("data",dataList);

        Map<String,Object> itemStyleMap = new HashMap<>();
        itemStyleMap.put("shadowBlur",10);
        itemStyleMap.put("shadowOffsetX",0);
        itemStyleMap.put("shadowColor","rgba(0, 0, 0, 0.5)");

        seriesListMapObj.put("emphasis",itemStyleMap);

        List<Map<String,Object>> seriesListMap = new ArrayList<>();
        seriesListMap.add(seriesListMapObj);
        optionMap.put("series",seriesListMap);

        return optionMap;
    }

    /**
     * 主单机场走货主单数
     * @param startDate
     * @param endDate
     * @return
     */
    public Object showFlightAirportOpenBill(String startDate, String endDate) {
        List<Map<String,Object>> mapList = bMasterBillFlightService.queryFlightAirportOpenBillCollectList(startDate,endDate);

        Map<String,Object> optionMap = new HashMap<>();

        Map<String,Object> titleMap = new HashMap<>();
        titleMap.put("text","机场走货主单量统计");
        titleMap.put("subtext",startDate + " 至 " + endDate);
        titleMap.put("left","center");
        optionMap.put("title",titleMap);

        Map<String,Object> tooltipMap = new HashMap<>();
        titleMap.put("trigger","item");
        optionMap.put("tooltip",tooltipMap);

        Map<String,Object> toolBoxMap = new HashMap<>();
        Map<String,Object> featureMap = new HashMap<>();
        featureMap.put("saveAsImage",new HashMap<String,Object>());
        toolBoxMap.put("feature",featureMap);
        optionMap.put("toolbox",toolBoxMap);

        Map<String,Object> legendMap = new HashMap<>();
        legendMap.put("orient","vertical");
        legendMap.put("left","left");
        optionMap.put("legend",legendMap);

        List<Map<String,Object>> dataList = new ArrayList<>();
        for(Map<String,Object> mapObj : mapList){
            Map<String,Object> dataMapObj = new HashMap<>();
            String airlinesName = DataTypeUtil.toStringObject(mapObj.get("name"));
            Integer totalNumber = DataTypeUtil.toInt(mapObj.get("totalNumber"));
            dataMapObj.put("name",airlinesName);
            dataMapObj.put("value",totalNumber);
            dataList.add(dataMapObj);
        }

        Map<String,Object> seriesListMapObj = new HashMap<>();
        seriesListMapObj.put("name","始发机场");
        seriesListMapObj.put("type","pie");
        seriesListMapObj.put("radius","50%");
        seriesListMapObj.put("data",dataList);

        Map<String,Object> itemStyleMap = new HashMap<>();
        itemStyleMap.put("shadowBlur",10);
        itemStyleMap.put("shadowOffsetX",0);
        itemStyleMap.put("shadowColor","rgba(0, 0, 0, 0.5)");

        seriesListMapObj.put("emphasis",itemStyleMap);

        List<Map<String,Object>> seriesListMap = new ArrayList<>();
        seriesListMap.add(seriesListMapObj);
        optionMap.put("series",seriesListMap);

        return optionMap;
    }
}
